﻿using FastCloud.Base.Desgin;
using FastCloud.Base.Entity;
using FastORM.Entity;
using FastUtil.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCloud.Base.DB
{
    public class DB_AttachConfig : BaseDBML<Frame_AttachConfig>
    {
        public override void Insert(Frame_AttachConfig Entity)
        {
            if (string.IsNullOrEmpty(Entity.ConfigGuid))
            {
                Entity.ConfigGuid = GenerateUtil.GenerateUnitGuid();
            }
            Entity.AddDate = DateTime.Now;
            base.Insert(Entity);
            if (Entity.Enable == (int)Base.Enum.Common.EnableOrDisable.启用)
            {
                DisableOtherConfig(Entity.ConfigGuid);
            }
        }

        public override void UpdateByID(Frame_AttachConfig Entity)
        {
            base.UpdateByID(Entity);
            if (Entity.Enable == (int)Base.Enum.Common.EnableOrDisable.启用)
            {
                DisableOtherConfig(Entity.ConfigGuid);
            }
        }

        public override void InsertBatch(List<Frame_AttachConfig> EntityList)
        {
            foreach (Frame_AttachConfig Entity in EntityList)
            {
                if (string.IsNullOrEmpty(Entity.ConfigGuid))
                {
                    Entity.ConfigGuid = GenerateUtil.GenerateUnitGuid();
                }
                Entity.AddDate = DateTime.Now;
            }
            base.InsertBatch(EntityList);
        }

        public bool ExistConfig(string ConfigName)
        {
            List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "ConfigName", ParamValue = ConfigName } };
            int count = SelectCount(string.Format("ConfigName=@ConfigName"), ParamList);
            return count > 0;
        }

        public bool ExistConfig(string ConfigGuid, string ConfigName)
        {

            List<ParamEntity> ParamList = new List<ParamEntity>()
            {
                new ParamEntity() { ParamName = "ConfigName", ParamValue = ConfigName },
                new ParamEntity() { ParamName = "ConfigGuid", ParamValue = ConfigGuid }
            };
            int count = SelectCount(string.Format("ConfigName=@ConfigName and ConfigGuid!=@ConfigGuid"), ParamList);
            return count > 0;
        }

        public void DisableOtherConfig(string ConfigGuid)
        {
            List<ParamEntity> ParamList = new List<ParamEntity>()
            {
                new ParamEntity() { ParamName = "Enable", ParamValue = (int)Base.Enum.Common.EnableOrDisable.禁用 },
                new ParamEntity() { ParamName = "ConfigGuid", ParamValue = ConfigGuid }
            };
            string sql = "Update frame_attach_config set Enable=@Enable where ConfigGuid!=@ConfigGuid";
            DTO.ExecuteSQL(sql, ParamList);
        }

        public Frame_AttachConfig SelectEnableConfig()
        {
            List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "Enable", ParamValue = (int)Base.Enum.Common.EnableOrDisable.启用 } };
            List<Frame_AttachConfig> ConfigList = SelectAllList("*", "Enable=@Enable", ParamList);
            if (ConfigList.Count>0)
            {
                return ConfigList[0];
            }
            else
            {
                return null;
            }
        }
    }
}
